package com.lee.lxl.idcard.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.lee.lxl.idcard.R;
import com.lee.lxl.idcard.tools.LoadToast;
import com.lee.lxl.idcard.tools.ProgressDialog;
import com.lee.lxl.idcard.urls.Urls;
import com.zhy.autolayout.AutoLayoutActivity;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class IDcardActivity extends AutoLayoutActivity {


    @Bind(R.id.et_idcard)
    EditText etIdcard;
    @Bind(R.id.bt_idcard_onclick)
    Button btIdcardOnclick;
    @Bind(R.id.tv_idcard)
    TextView tvIdcard;

    private AbHttpUtil mAbHttpUtil = null;
    final String text = "123";
    private LoadToast lt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_idcard);
        ButterKnife.bind(this);
        mAbHttpUtil = AbHttpUtil.getInstance(this);
        mAbHttpUtil.setTimeout(10000);
        mAbHttpUtil.setEncode("UTF-8");

        lt = new LoadToast(this).setText(text).setTranslationY(100);
    }

    @OnClick(R.id.bt_idcard_onclick)
    public void onClick() {
        // 绑定参数
        AbRequestParams params = new AbRequestParams();
        params.put("key", "3222b198b0ba29039843092f13f592a7");
        params.put("cardno",etIdcard.getText().toString());
        mAbHttpUtil.get(Urls.IDCARD_URL, params, new AbStringHttpResponseListener() {
            @Override
            public void onSuccess(int i, String s) {
                try {
                    Log.e("IDcardActivity", s);
                    JSONObject jsonObject = new JSONObject(s);
                    if(jsonObject.getString("resultcode").equals("200")){
                        Toast.makeText(IDcardActivity.this, jsonObject.getString("reason"), Toast.LENGTH_SHORT).show();
                        JSONObject object = jsonObject.getJSONObject("result");
                        tvIdcard.setText(object.getString("area") + "\n" + object.getString("sex")  + "\n" + object.getString("birthday"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onStart() {
                lt.show();
            }

            @Override
            public void onFinish() {
                lt.success();
            }

            @Override
            public void onFailure(int i, String s, Throwable throwable) {
                Log.e("IDcardActivity", s);
                lt.error();
            }
        });
    }
}
